package com.dragon.blelibrary.manager;

import java.util.Objects;

/**
 * 蓝牙连接的参数
 */
public class BleConnectParam {
    /**蓝牙地址*/
    private String address;
    /**
     * 重连次数，
     * 大于0，表示重连次数，可以配合是否需要重开系统蓝牙{@link BleConnectParam#reopenSysBle}一起使用
     * 等于0，表示不重连
     * 默认不重连
     *
     */
    private int reconnection;
    /**
     * 重连间隔时间，毫秒
     * 默认 600毫秒
     */
    private long reconnectionIntervals = 600;
    /**
     * 是否重开系统蓝牙，同时也表示系统蓝牙打开时，是否重连
     * 当重连若干次之后，还是连接不上的话，就是否重开一次系统蓝牙
     * true：前提条件，重连次数{@link BleConnectParam#reconnection}必须大于0.
     *      重开时机为重连次数的倒数第二第三次；
     * false:不重开
     * 默认：false
     */
    private boolean reopenSysBle;
    /**
     * 是否已经重启过系统蓝牙了，如果已经重启过，则无需再重启，只重启一次
     */
    private static boolean reopenBleAction;

    /**
     * 超时时间，毫秒
     * 当连接没事件（断开、连接、错误）返回，超时生效
     * 最小要50秒以上
     * 默认两分钟
     * 如果是一直重连{@link BleConnectParam#reconnection}，超时失效
     */
    private long timeOut = 2 * 60 * 1000;
    /**
     * 超时，是否包含重启系统蓝牙的时间
     * 主要针对当打开系统蓝牙对话框时，用户是否选择了确认或取消,与{@link BleConnectParam#timeOut}配合使用
     * true:一直等待用户点击，用户确认打开系统蓝牙后重新倒计时，用户取消打开系统蓝牙，则倒计时结束
     * false，不等用户点击，倒计时到点后就超时
     * 默认false
     */
    private boolean timeOutIncludeRestartSysBle;
    /**
     * 超时到点后，如果此时已经连接上了，是否需要断开它
     * 默认，不断开
     * true :断开
     * false:不断开
     * 主要是针对部分手机的handler移除超时无效时，还在继续计算超时，
     */
    private boolean timeOutDisconnected;
//    /**
//     * 超时后，是否继续重连
//     * 默认false
//     */
//    private boolean timeOutReconnect;
    /**
     * 是否是超时断开的
     * 调用者只获取，不赋值
     */
    private boolean timeOutDisconnect;
    /**
     * 每次重连搜索时间，毫秒(会触发多次)
     * 大于0，表示需要搜索的时间，
     * 等于0，表示，不搜索
     * 小于0，表示一直搜索,搜索时长为24小时,搜索到就停止搜索去连接
     * 默认，0毫秒
     */
    private long reconnectSearchTime ;
    /**
     * 是否触发一次搜索
     * 当{@link BleConnectParam#reconnectSearchTime}为0时才生效
     * 默认：true
     * 搜索时间为一秒钟
     * 如果已经触发了搜索，就会赋值未false
     */
    private boolean oneTimesScan = true;

    /**
     * 断开次数，调用者无需赋值
     */
    private int disConnectTimes;
    /**
     * 是否连接过了
     * 只要连接成功过一次，则为true
     * 调用者无需赋值
     */
    private boolean connceted;

    /**
     * 系统蓝牙是否打开的
     */
    private boolean sysBleIsEnabled;
    /**
     * 当系统蓝牙打开时，是否连接，
     * 默认true
     */
    private boolean bleOpenConnect = true;
    /**
     * 连接时，当系统蓝牙未打开时，是否打开系统蓝牙
     * 默认true
     */
    private boolean connectOpenSysBle = true;
    /**
     * 连接方式，钩子模式，由调用者赋值取值，用于连接成功或断开后判断类型
     */
    private ConnectType connectType;
    /**
     * 是否开过飞行模式了
     */
    private static boolean flightMode;

    /**
     * 对于有需要绑定的设备时，在绑定中的状态时，
     * 是否忽略这个状态而继续去发现服务（主要针对个别需要绑定的设备比较难连接的优化）
     * true:在绑定中也继续去发现服务
     * false:等待绑定完成后再去发现服务
     * 默认（false）
     */
    private boolean bondIngDiscoverServices = false;

    public boolean isBondIngDiscoverServices() {
        return bondIngDiscoverServices;
    }

    public void setBondIngDiscoverServices(boolean bondIngDiscoverServices) {
        this.bondIngDiscoverServices = bondIngDiscoverServices;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getReconnection() {
        return reconnection;
    }

    public void setReconnection(int reconnection) {
        this.reconnection = reconnection;
    }

    public long getReconnectionIntervals() {
        return reconnectionIntervals;
    }

    public void setReconnectionIntervals(long reconnectionIntervals) {
        this.reconnectionIntervals = reconnectionIntervals;
    }

    public boolean isReopenSysBle() {
        return reopenSysBle;
    }

    public void setReopenSysBle(boolean reopenSysBle) {
        this.reopenSysBle = reopenSysBle;
    }

    public long getTimeOut() {
        return timeOut;
    }

    public void setTimeOut(long timeOut) {
        if(timeOut < 50 * 1000)
            this.timeOut = 50 * 1000;
        else
            this.timeOut = timeOut;
    }

    public boolean isTimeOutIncludeRestartSysBle() {
        return timeOutIncludeRestartSysBle;
    }

    public void setTimeOutIncludeRestartSysBle(boolean timeOutIncludeRestartSysBle) {
        this.timeOutIncludeRestartSysBle = timeOutIncludeRestartSysBle;
    }

    public long getReconnectSearchTime() {
        return reconnectSearchTime;
    }

    public void setReconnectSearchTime(long reconnectSearchTime) {
        this.reconnectSearchTime = reconnectSearchTime;
    }

    public  int getDisConnectTimes() {
        return disConnectTimes;
    }

     void setDisConnectTimes(int disConnectTimes) {
        this.disConnectTimes = disConnectTimes;
    }

    public static boolean isReopenBleAction() {
        return reopenBleAction;
    }

    public static void setReopenBleAction(boolean reopenBleAction) {
        BleConnectParam.reopenBleAction = reopenBleAction;
    }

   public boolean isConnceted() {
        return connceted;
    }

    void setConnceted(boolean connceted) {
        this.connceted = connceted;
    }

    public boolean isTimeOutDisconnect() {
        return timeOutDisconnect;
    }

    void setTimeOutDisconnect(boolean timeOutDisconnect) {
        this.timeOutDisconnect = timeOutDisconnect;
    }

    public boolean isSysBleIsEnabled() {
        return sysBleIsEnabled;
    }

    void setSysBleIsEnabled(boolean sysBleIsEnabled) {
        this.sysBleIsEnabled = sysBleIsEnabled;
    }

    public boolean isBleOpenConnect() {
        return bleOpenConnect;
    }

    public void setBleOpenConnect(boolean bleOpenConnect) {
        this.bleOpenConnect = bleOpenConnect;
    }

    public boolean isConnectOpenSysBle() {
        return connectOpenSysBle;
    }

    public void setConnectOpenSysBle(boolean connectOpenSysBle) {
        this.connectOpenSysBle = connectOpenSysBle;
    }


    public boolean isOneTimesScan() {
        return oneTimesScan;
    }

    public void setOneTimesScan(boolean oneTimesScan) {
        this.oneTimesScan = oneTimesScan;
    }

    public ConnectType getConnectType() {
        return connectType;
    }

    public void setConnectType(ConnectType connectType) {
        this.connectType = connectType;
    }

    public static boolean isFlightMode() {
        return flightMode;
    }

    public static void setFlightMode(boolean flightMode) {
        BleConnectParam.flightMode = flightMode;
    }

    public boolean isTimeOutDisconnected() {
        return timeOutDisconnected;
    }

    public void setTimeOutDisconnected(boolean timeOutDisconnected) {
        this.timeOutDisconnected = timeOutDisconnected;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BleConnectParam that = (BleConnectParam) o;
        return Objects.equals(address, that.address);
    }



    @Override
    public int hashCode() {

        return Objects.hash(address);
    }

    /**
     * 连接方式，搜索连，重连，OTA连……
     */
    public interface ConnectType{}
    @Override
    public String toString() {
        return  "\n 连接参数配置BleConnectParam" +
                "\n address = " + address +
                "\n 重连次数    reconnection = " + reconnection +
                "\n 重连间隔时间  reconnectionIntervals = " + reconnectionIntervals +
                "\n 是否重开系统蓝牙    reopenSysBle = " + reopenSysBle +
                "\n 超时时间    timeOut=" + timeOut +
                "\n 超时，是否包含重启系统蓝牙的时间    timeOutIncludeRestartSysBle = " + timeOutIncludeRestartSysBle +
                "\n 重连搜索时间  reconnectSearchTime = " + reconnectSearchTime +
                "\n 断开次数    disConnectTimes = " + disConnectTimes +
                "\n 是否连接过了  connceted = " + connceted +
                "\n 是否是超时断开的  timeOutDisconnect = " + timeOutDisconnect +
                "\n 系统蓝牙是否打开  sysBleIsEnabled = " + sysBleIsEnabled +
                "\n 系统蓝牙打开就连接  bleOpenConnect = " + bleOpenConnect +
                "\n 连接时打开系统蓝牙  connectOpenSysBle = " + connectOpenSysBle +
                "\n 触发搜索一次  oneTimesScan = " + oneTimesScan +
                "\n 连接方式  connectType = " + connectType +
                "\n 开过飞行模式  flightMode = " + flightMode ;
    }
}
